package org.cbg.panel;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.io.File;
import java.io.*;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import cn.hutool.core.bean.BeanUtil;
import org.cbg.btn.CBGMySaleBtn;
import org.cbg.entity.Salegoods;
import org.cbg.entity.SalegoodsExt;
import org.cbg.frame.TrslationMainJframe;
import org.cbg.mouslisten.CBGmoveMouslisten;
import org.come.socket.Agreement;
import org.come.socket.SendMessageUntil;
import org.come.until.CutButtonImage;
import org.come.until.GsonUtil;
import org.come.until.UserData;

import com.tool.tcpimg.UIUtils;

 

/**
 * 我要买大话币模型面板
 * 
 * @author Administrator
 * 
 */

public class TraslationWantSentYijishangpinModelJpanel extends JPanel {
    // 展示底框 展示图标 大话币上 大话币下 价格 详情
    private JLabel zhanshidikuang, tupian, dahuabishang, dahuabixia, danjia, price;
    public SalegoodsExt salegoods;
    public CBGMySaleBtn  mySaleBtn2,fahuo, sale_quhui,shoukuan; //mySaleBtn1
    private CBGmoveMouslisten cbGmoveMouslisten;

    /**
     * 将商品信息整合到界面
     *
     * @param salegoods
     */
    public TraslationWantSentYijishangpinModelJpanel(Salegoods salegoods) {
        this.setLayout(null);
        this.setPreferredSize(new Dimension(740, 62));
        this.setOpaque(false);

        tupian = new JLabel();
        // buy=new JButton();
        zhanshidikuang = new JLabel();
        dahuabishang = new JLabel();
        dahuabixia = new JLabel();
        danjia = new JLabel();
        zhanshidikuang.setBorder(null);
        zhanshidikuang.setBounds(38, 2, 53, 53);
        zhanshidikuang.setIcon(CutButtonImage.getImage("inkImg/number/b4/spbj.png", 53, 53));
        zhanshidikuang.setOpaque(false);
        this.add(tupian);
        this.add(zhanshidikuang);

        price = new JLabel();
        dahuabixia.setFont(UIUtils.TEXT_FONT1);
        dahuabixia.setForeground(UIUtils.COLOR_cbg3);
        //        dahuabixia.setHorizontalAlignment(SwingConstants.CENTER);
        dahuabishang.setFont(UIUtils.TEXT_FONT2);
        dahuabishang.setForeground(UIUtils.COLOR_cbg6);
        dahuabishang.setHorizontalAlignment(SwingConstants.CENTER);
        danjia.setFont(UIUtils.TEXT_FONT2);
        danjia.setForeground(UIUtils.COLOR_cbg6);
        danjia.setHorizontalAlignment(SwingConstants.CENTER);
        price.setFont(UIUtils.TEXT_FONT60);
        price.setForeground(UIUtils.COLOR_cbg4);
        price.setHorizontalAlignment(SwingConstants.CENTER);
        cbGmoveMouslisten = new CBGmoveMouslisten(salegoods.getSaletype(), salegoods.getOtherid());
        tupian.addMouseListener(cbGmoveMouslisten);
        tupian.setBounds(40, 5, 48, 48);
        dahuabixia.setBounds(100, 8, 135, 24);
        dahuabishang.setBounds(100, 30, 100, 24);
        price.setBounds(200, 12, 125, 40);
        danjia.setBounds(360, 12, 125, 40);

        // mySaleBtn1 = new CBGMySaleBtn("skin/8100226/button/2.png", 1, UIUtils.COLOR_BTNTEXT, UIUtils.TEXT_FONT, 5, "上架", this);
        // mySaleBtn1.setName("上架");
        // mySaleBtn1.setBounds(455, 15, 43, 17);
        mySaleBtn2 = new CBGMySaleBtn("inkImg/number/b4/kbaj.png", 1, UIUtils.COLOR_BTNPUTONG1, UIUtils.TEXT_HY166, 5, "下架", this);
        mySaleBtn2.setBounds(540, 15, 57, 26);
        mySaleBtn2.setName("下架");
        //this.add(mySaleBtn1);

        fahuo = new CBGMySaleBtn("inkImg/number/b4/kbaj.png", 1, UIUtils.COLOR_BTNPUTONG1, UIUtils.TEXT_FONT, 6, "确认发货", this);
        fahuo.setBounds(540, 15, 57, 26);
        fahuo.setVisible(false);
        this.add(fahuo);

        shoukuan = new CBGMySaleBtn("inkImg/number/b4/kbaj.png", 1, UIUtils.COLOR_BTNPUTONG1, UIUtils.TEXT_FONT, 7, "确认收款", this);
        shoukuan.setBounds(540, 15, 57, 26);
        shoukuan.setVisible(false);
        this.add(shoukuan);

        //买家不付款，取回
        sale_quhui = new CBGMySaleBtn("inkImg/number/b4/kbaj.png", 1, UIUtils.COLOR_BTNPUTONG1, UIUtils.TEXT_FONT, 8, "取回", this);
        sale_quhui.setBounds(610, 15, 57, 26);
        sale_quhui.setVisible(false);
        this.add(sale_quhui);

        this.add(mySaleBtn2);
        this.add(dahuabixia);
        this.add(dahuabishang);
        this.add(danjia);
        this.add(price);
        SX(salegoods);
    }

    /** 面板刷新 */
    public void SX(Salegoods salegoods) {
        fahuo.setVisible(false);
        sale_quhui.setVisible(false);
        shoukuan.setVisible(false);
        SalegoodsExt salegoodsExt = new SalegoodsExt();
        BeanUtil.copyProperties(salegoods,salegoodsExt);
        this.salegoods = salegoodsExt;
        tupian.setIcon(CutButtonImage.getCBG(salegoods.getSaletype(), salegoods.getSaleskin(), 48, 48));
        dahuabishang.setText(salegoods.getSalename());
        cbGmoveMouslisten.qh(salegoods.getSaletype(), salegoods.getOtherid());
        if (salegoods.getSaletype() == 2) {
            dahuabixia.setText(UserData.getMS(salegoods.getOtherid()));
        } else {
            dahuabixia.setText(null);
        }
        if (salegoods.getFlag() == 1) {
            danjia.setText("已下架");
        } else if (salegoods.getFlag() == 2) {
            danjia.setText("已上架");
        }
        else if (salegoods.getFlag() == 5) {
            danjia.setText("已收款");
        }
        else if (salegoods.getFlag() == 3 || salegoods.getFlag() == 4) {
            danjia.setText("待发货");
            fahuo.setVisible(true);
            sale_quhui.setVisible(true);
        } else if (salegoods.getFlag() == 7) {
            danjia.setText("已取出");
        } else if (salegoods.getFlag() == 6) {
            shoukuan.setVisible(true);
            danjia.setText("待确认");
        } else {
            danjia.setText(null);
        }
        price.setText(salegoods.getSaleprice() + "");
        //mySaleBtn1.setVisible(salegoods.getFlag() == 1 ? true : false);
        if (salegoods.getFlag() == 2) {
            try {
                mySaleBtn2.setText("下架");
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            mySaleBtn2.setName("下架");
            mySaleBtn2.setVisible(true);
        } else if (salegoods.getFlag() == 1) {
            try {
                mySaleBtn2.setText("取回");
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            mySaleBtn2.setName("取回");
            mySaleBtn2.setVisible(true);
        } else {
            mySaleBtn2.setVisible(false);
        }
    }

    /** 上架下架取回操作 */
    public void caozuo(String type) {
        Salegoods salegoods = new Salegoods();
        salegoods.setSaleid(this.salegoods.getSaleid());
        if ("上架".equals(type)) {
            //mySaleBtn1.setVisible(false);
            try {
                mySaleBtn2.setText("下架");
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            mySaleBtn2.setName("下架");
            salegoods.setFlag(2);
        } else if ("下架".equals(type)) {
            try {
                mySaleBtn2.setText("取回");
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            mySaleBtn2.setName("取回");
            //mySaleBtn1.setVisible(true);
            salegoods.setFlag(1);
        } else if ("取回".equals(type)) {
            TraslationWantSendCardJpanel traslationWantSendCardJpanel = TrslationMainJframe.getTrslationMainJframe().getTrslationMainJpanel().getTranslationMainCardJpanel().getTraslationWantSentJpanel().getTraslationWantSendCardJpanel();
            traslationWantSendCardJpanel.getTraslationWantSentYijishangpinJpanel().removeJpanel(this);
            salegoods.setFlag(5);
        }
        String sendmes = Agreement.getAgreement().searchOperationGoodAgreement(
                GsonUtil.getGsonUtil().getgson().toJson(salegoods));
        SendMessageUntil.toServer(sendmes);
    }

    private ImageIcon icon;

    @Override
    protected void paintComponent(Graphics g) {
        // TODO Auto-generated method stub
        super.paintComponent(g);
        g.setColor(new Color(245, 245, 245));
        if (icon == null) {
            icon = CutButtonImage.getImage("inkImg/old/1/Z1001.png", 740, 2);
        }
        g.drawImage(icon.getImage(), 0, 58, 740, 2, this);



    }
}
